import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Categories from '../components/Commodity/Categories.vue'
import goods from '../components/Commodity/Goods.vue'
import Params from '../components/Commodity/Params.vue'
import add from '../components/Commodity/Add.vue'
import orders from '../components/Orders/orders.vue'
import reports from '../components/reports/reports.vue'
Vue.use(VueRouter)

const routes = [
  // 重定向
  { path: '/', redirect: '/login' },
  // 导入路由
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/Welcome',
    children: [
      { path: '/Welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Categories },
      { path: '/params', component: Params },
      { path: '/goods', component: goods },
      { path: '/goods/add', component: add },
      {
        path: '/orders',
        component: orders
      },
      {
        path: '/reports',
        component: reports
      }

    ]
  }
]

const router = new VueRouter({
  routes
})
// 挂载路由守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路由
  // from代表从那个路径条转而来
  // next 是一个函数 表示放行
  //  next()  放行 next("/login") 强制条转

  if (to.path === '/login') return next()
  // 获取 token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
